package cac.fenjian.resource.mscarbasic.service;

import cac.common.DateUtil;
import cac.fenjian.resource.MsCarBasic.dto.MsCarBasicDTO;
import cac.fenjian.resource.WmsReturnApply.dto.WmsReturnApplyDTO;
import cac.fenjian.resource.mscarbasic.dao.MsCarBasicDao;
import com.cac.dme.adapter.rest.msg.QueryReqBean;
import com.cac.dme.adapter.rest.msg.QueryRespBean;
import com.cac.dme.adapter.utils.ComUtil;
import com.cac.dme.adapter.utils.PageHelper;
import com.cac.dme.adapter.utils.PojoUtil;
import com.cac.dme.common.exception.service.DaoException;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class MsCarBasicService {
    @Autowired
    private MsCarBasicDao msCarBasicDao;
    Date date = new Date();
    SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String nowTime = sf.format(date);
    Date time = sf.parse(nowTime);

    public MsCarBasicService() throws ParseException {
    }

    /**
     * 按照条件分页查询
     * @param queryReqBean
     * @param orderBy
     */
    public QueryRespBean<HashMap> searchMsCarBasicByPage(QueryReqBean<HashMap> queryReqBean, String orderBy, String Keywords){
        QueryRespBean<HashMap> queryRespBean =new QueryRespBean<>();
        try {
            PageHelper.startPage(queryReqBean.getPageParameter());
            HashMap searchParams = queryReqBean.getSearchParams();
            Page<HashMap> dataList = msCarBasicDao.searchMsCarBasicByPage(searchParams,orderBy,Keywords);
            queryRespBean.setResult(dataList);
            return queryRespBean;
        }catch (Exception e) {
            throw new DaoException(e.getMessage(), e);
        }
    }
    //判断查询是否为空
    public List<HashMap> searchMsCarBasicByPage(HashMap searchParams, String orderBy, String Keywords){
        try {
            List<HashMap> dataList = msCarBasicDao.searchMsCarBasicByPage(searchParams,orderBy,Keywords);
            return dataList;
        }catch (Exception e) {
            throw new DaoException(e.getMessage(), e);
        }
    }
    /**
     * 根据条件查询数据
     * @param queryReqBean
     * @return
     */
    public List<MsCarBasicDTO> searchMsCarByParams(QueryReqBean<MsCarBasicDTO> queryReqBean){
        try{
            return msCarBasicDao.searchMsCarByParams(queryReqBean.getSearchParams());
        }catch (Exception e){
            throw new  DaoException(e.getMessage(),e);
        }
    }
    /**
     * 通过主键查询单条记录
     *
     * @param id 主键id
     * @return EmployeeBasic1DTO
     */
    @Transactional
    public MsCarBasicDTO findMsCarBasicById(String id) {
        try {
            MsCarBasicDTO msCarBasicDTO = msCarBasicDao.findMsCarBasicById(id);
            //记录日志
            if (msCarBasicDTO != null) {
            }
            return msCarBasicDTO;
        } catch (Exception e) {
            throw new DaoException(e.getMessage(), e);
        }
    }
    /**
     * 新增单个数据
     */
    @Transactional
    public int insertMsCarBasic(Map map) {
        try{
            int count = msCarBasicDao.insertMsCarBasicDTO(map,ComUtil.getId(),DateUtil.nowTime());
            return count;
        }catch (Exception e){
            throw new DaoException(e.getMessage(),e);
        }
    }
    /**
     *修改数据
     * @param
     * @return
     */
    @Transactional
    public int updateMsCarBasic(Map map){

        try{
            int count = msCarBasicDao.updateMsCarDTO(map,DateUtil.nowTime());
            if(count==0){
                throw new DaoException("修改数据失败");
            }
            return count;
        }catch (Exception e){
            throw new DaoException(e.getMessage(),e);
        }
    }
    /**
     * 按主键单条删除
     *
     * @param id
     * @return
     */
    @Transactional
    public int deleteMsCarBasicById(String id) {
        if (StringUtils.isEmpty(id)) {
            throw new DaoException("删除失败！传入的参数主键为null");
        }
        try {
            //记录日志
            MsCarBasicDTO msCarBasicDTO = msCarBasicDao.findMsCarBasicById(id);
            if (msCarBasicDTO == null) {
                throw new DaoException("删除失败！对象不存在");
            }
    //删除业务数据
            int count = msCarBasicDao.deleteMsCarBasicById(id);
            return count;
        } catch (Exception e) {
            throw new DaoException(e.getMessage(), e);
        }
    }
}
